﻿Imports System.Windows.Forms
Public Class StringListEditor
  Public Sub New()
    InitializeComponent()
  End Sub

  Public Sub New(ByVal newStrings As List(Of String))
    Me.New()
    Me.Strings = newStrings
    Me.InputPanelMain_EnabledChanged(Me, EventArgs.Empty)
  End Sub

  Public Overrides Property BackColor() As System.Drawing.Color
    Get
      Return MyBase.BackColor
    End Get
    Set(ByVal value As System.Drawing.Color)
      MyBase.BackColor = value
      Me.Panel1.BackColor = value
      Me.WP7ButtonAdd.BackColor = value
      Me.WP7ButtonRemove.BackColor = value
      Me.ListBoxStrings.BackColor = value
      Me.TextBoxString.BackColor = value
    End Set
  End Property

  Public Overrides Property ForeColor() As System.Drawing.Color
    Get
      Return MyBase.ForeColor
    End Get
    Set(ByVal value As System.Drawing.Color)
      MyBase.ForeColor = value
      Me.WP7ButtonAdd.ForeColor = value
      Me.WP7ButtonRemove.ForeColor = value
      Me.ListBoxStrings.ForeColor = value
      Me.TextBoxString.ForeColor = value
    End Set
  End Property

  Public Property Strings() As List(Of String)
    Get
      Dim fs As New List(Of String)
      For Each s As String In Me.ListBoxStrings.Items
        fs.Add(s)
      Next
      Return fs
    End Get
    Set(ByVal value As List(Of String))
      Me.ListBoxStrings.Items.Clear()
      For Each f As String In value
        Me.ListBoxStrings.Items.Add(f.Trim)
      Next
      Me.ListBoxSort()
    End Set
  End Property

  Public Sub FolderListEditor_Load() Handles Me.Load
    Me.WP7ButtonAdd.Text = Language.GetLanguageString("Add")
    Me.WP7ButtonRemove.Text = Language.GetLanguageString("Remove")
    Me.MenuItemOK.Text = Language.GetLanguageString("OK")
    Me.MenuItemCancel.Text = Language.GetLanguageString("Cancel")
    Me.TimerFocus.Enabled = True
  End Sub

  Private Sub WP7ButtonAdd_MouseUp(ByVal sender As System.Object, ByVal e As MouseEventArgs) Handles WP7ButtonAdd.MouseUp
    Me.TextBoxString.Text = Me.TextBoxString.Text.Trim
    If Me.TextBoxString.Text.Length > 0 Then
      Me.ListBoxStrings.Items.Add(Me.TextBoxString.Text)
      Me.TextBoxString.Text = ""
      Me.ListBoxSort()
    End If
  End Sub

  Private Sub WP7ButtonRemove_MouseUp(ByVal sender As System.Object, ByVal e As MouseEventArgs) Handles WP7ButtonRemove.MouseUp
    If Me.ListBoxStrings.SelectedIndex >= 0 Then
      Dim si As Integer = Me.ListBoxStrings.SelectedIndex
      Me.ListBoxStrings.Items.RemoveAt(Me.ListBoxStrings.SelectedIndex)
      If si < Me.ListBoxStrings.Items.Count Then
        Me.ListBoxStrings.SelectedIndex = si
      ElseIf si > 0 AndAlso si - 1 < Me.ListBoxStrings.Items.Count Then
        Me.ListBoxStrings.SelectedIndex = si - 1
      ElseIf Me.ListBoxStrings.Items.Count > 0 Then
        Me.ListBoxStrings.SelectedIndex = 0
      End If
    End If
  End Sub

  Private Sub MenuItemOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItemOK.Click
    Me.DialogResult = Windows.Forms.DialogResult.OK
    Me.Close()
  End Sub

  Private Sub MenuItemCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItemCancel.Click
    Me.DialogResult = Windows.Forms.DialogResult.Cancel
    Me.Close()
  End Sub

  Private Sub ListBoxStrings_GotFocus() Handles ListBoxStrings.GotFocus
    ' Makes sure we put the focus back on the text box
    Me.TimerFocus.Enabled = True
  End Sub

  Private Sub TextBoxString_KeyUp(ByVal sender As Object, ByVal e As KeyEventArgs) Handles TextBoxString.KeyUp
    If e.KeyCode = Keys.Enter Then
      Me.WP7ButtonAdd_MouseUp(Me, New MouseEventArgs(Windows.Forms.MouseButtons.Left, 1, 0, 0, 0))
    End If
  End Sub

  Private Sub ListBoxSort()
    Dim arr As New ArrayList
    Dim i As Integer
    arr.AddRange(ListBoxStrings.Items)
    arr.Sort()
    ListBoxStrings.Items.Clear()
    For i = 0 To arr.Count - 1
      ListBoxStrings.Items.Add(arr(i))
    Next i
    If Me.ListBoxStrings.Items.Count > 0 Then
      Me.ListBoxStrings.SelectedIndex = 0
    End If
  End Sub

  Private Sub TimerFocus_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerFocus.Tick
    Me.TimerFocus.Enabled = False
    Me.TextBoxString.Focus()
  End Sub

  Private Sub InputPanelMain_EnabledChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles InputPanelMain.EnabledChanged
    If Me.InputPanelMain.Enabled Then
      Me.PanelPushUp.Height = Me.InputPanelMain.Bounds.Height
    Else
      Me.PanelPushUp.Height = 0
    End If
  End Sub
End Class